import java.util.ArrayList;

public class Library {
    private ArrayList<LibraryItem> bookList;
    private ArrayList<LibraryItem> movieList;
    private LibraryUsers userSystem;
    private User activeUser = null;

    public Library() {
        bookList = new ArrayList<LibraryItem>();
        movieList = new ArrayList<LibraryItem>();
        userSystem = new LibraryUsers();
    }

    private void printList(ArrayList<LibraryItem> list){
        System.out.println();
        for(int i = 0; i < list.size(); i++){
            System.out.println(list.get(i));
        }
        System.out.println();
    }

    public void printListOfBooks(){
        printList(bookList);
    }

    public void printListOfMovies(){
        printList(movieList);
    }

    public void addBook(Book book) {
        bookList.add(book);
    }
    public void addMovie(Movie movie) {
        movieList.add(movie);
    }

    public Book getBookToReserve(String target) {
        for(int i = 0; i < bookList.size(); i++){
            if( bookList.get(i).getName().equals(target) )
                return (Book)(bookList.get(i));
        }
        return null;
    }

    public void attemptToReserve(String target){
        Book bookToReserve = getBookToReserve(target);

        if(bookToReserve != null )
            bookToReserve.reserve();
        else
            System.out.println("Sorry we don't have that book yet.");
    }

    //The validateUser function will do most of the work, and ask the User object whether the given password is correct
    //This function will just set the active user in the library if all of the rest works
    public void attemptLogin(String username, String password){
        User user = userSystem.validateUser(username,password);
        if(user!=null){
            activeUser=user;
            System.out.println("Login successful!");
        }
    }

    public boolean isUserActive(){
        if(this.activeUser != null)
            return true;
        return false;
    }

    public void printActiveUserDetails(){
        System.out.println();
        System.out.println("Name :\t"+activeUser.getName());
        System.out.println("Email :\t"+activeUser.getEmail());
        System.out.println("Phone :\t"+activeUser.getPhone());
        System.out.println();
    }

}
